/**
 * Copyright (c) 2010 Bob Brady (http://www.bradysoftware.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the 
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 
 * express or implied. See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package com.bradysoftware.rsscenturion;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.sun.syndication.feed.synd.SyndFeed;

public class ForEachEntryTag extends SimpleTagSupport {

	private String feedId;
	private Integer startIndex;
	private Integer endIndex;

	private int start;
	private int end;
	private int index;

	public void setFeedId(String feedId) {
		this.feedId = feedId;
	}

	public void setStartIndex(Integer startIndex) {
		this.startIndex = startIndex;
	}

	public void setEndIndex(Integer endIndex) {
		this.endIndex = endIndex;
	}

	@Override
	public void doTag() throws JspException, IOException {
		SyndFeed feed = (SyndFeed) getJspContext().getAttribute(feedId);

		if (feed != null && feed.getEntries().size() > 0) {
			start = startIndex;
			end = endIndex;
			if (end < start) {
				end = start;
			}
			for (index = start; index <= end; index++) {
				getJspContext().setAttribute("index", index);
				getJspContext().setAttribute("entry", feed.getEntries().get(index));
				getJspBody().invoke(null);
			}
		}
	}
}
